//
//  HRTouchManager.m
//  3DTouch
//
//  Created by duanhuifen on 2017/7/20.
//  Copyright © 2017年 huifen. All rights reserved.
//

#import "HRTouchManager.h"
#import <UIKit/UIKit.h>

@interface HRTouchManager ()

@property (nonatomic,strong) NSArray *textArr;

@end

@implementation HRTouchManager
+ (HRTouchManager*)sharedManager
{
    //Singleton instance
    static HRTouchManager *manager;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        manager = [[self alloc] init];
    });
    
    return manager;
}

- (void)setTitleArr:(NSArray *)titleArr{
    _textArr = titleArr;
    [self creatShortCutsWithTitleArr:titleArr andIconArr:nil];
}

- (void)setIconArr:(NSArray *)iconArr{
    [self updataShortCutsWithIconArr:iconArr];
}

- (void)creatShortCutsWithTitleArr:(NSArray *)titleArr andIconArr:(NSMutableArray *)iconArr{
    NSMutableArray *shortcutItems = [[NSMutableArray alloc]init];
    
    if (titleArr.count) {
        for (int i = 0; i < titleArr.count; i++) {
            NSString * titleStr = [NSString stringWithFormat:@"dev://%@",titleArr[i]];
            NSDictionary *info = @{@"scheme":titleStr};
            NSString * typeStr = [NSString stringWithFormat:@"com.risenb.%@",titleArr[i]];
            
            UIMutableApplicationShortcutItem *item = [[UIMutableApplicationShortcutItem alloc] initWithType:typeStr localizedTitle:titleArr[i] localizedSubtitle:@"" icon:nil userInfo:info];
            [shortcutItems addObject:item];
        }
    }
    NSArray * newItems = [[shortcutItems reverseObjectEnumerator] allObjects] ;
    [[UIApplication sharedApplication] setShortcutItems:newItems];
}

- (void)updataShortCutsWithIconArr:(NSArray *)iconArr{
    NSArray * newIcons = [[iconArr reverseObjectEnumerator] allObjects] ;

       // 将 shortcutItems 数组改为可变数组
    NSMutableArray *newShortcutItems = [[UIApplication sharedApplication].shortcutItems mutableCopy];
    
    for (int i = 0; i <newIcons.count; i++) {
        // 获取第 0 个 shortcutItem
        UIApplicationShortcutItem *shortcutItem = [[UIApplication sharedApplication].shortcutItems objectAtIndex:i];
        
        // 将 shortcutItem0 的类型由 UIApplicationShortcutItem 改为可修改类型 UIMutableApplicationShortcutItem
        UIMutableApplicationShortcutItem * newShortcutItem = [shortcutItem mutableCopy];
//        [newShortcutItem setLocalizedTitle:@"按钮1"];
        [newShortcutItem setIcon:[UIApplicationShortcutIcon iconWithTemplateImageName:newIcons[i]] ];
        // 替换原 ShortcutItem
        [newShortcutItems replaceObjectAtIndex:i withObject:newShortcutItem];
    }
    [UIApplication sharedApplication].shortcutItems = newShortcutItems;
}

- (void)turnWithTitle:(NSString *)title andReturnIndex:(void (^)(NSInteger))returnBlock{
    [_textArr enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isEqualToString:title]) {
            returnBlock(idx);
        }
    }];
}

- (NSArray *)textArr
{
    if (!_textArr) {
        _textArr = [NSArray array];
    }
    return _textArr;
}

@end
